package com.xiyu.service.repository.market.template;

import com.xiyu.service.enums.market.template.MarketTemplateTypeEnum;
import com.xiyu.service.model.market.template.*;
import com.xiyu.service.vo.market.marketTemplate.MarketTemplatePageQueryInput;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.Page;
import org.babyfish.jimmer.sql.ast.Predicate;
import org.springframework.util.StringUtils;

import java.util.Optional;
import java.util.List;

public interface MarketTemplateRepository extends JRepository<MarketTemplate, Long> {
    MarketTemplateTable marketTemplateTable = MarketTemplateTable.$;

    default Page<MarketTemplate> pageQuery(MarketTemplatePageQueryInput inputVO, List<Long> ownerIds){
        return sql().createQuery(marketTemplateTable)
                .whereIf(StringUtils.hasText(inputVO.getName()), () -> marketTemplateTable.name().like(inputVO.getName()))
                .whereIf(StringUtils.hasText(inputVO.getSubject()), () -> marketTemplateTable.subject().like(inputVO.getSubject()))
                .whereIf(inputVO.getType() != null, marketTemplateTable.type().eq(inputVO.getType()))
                .whereIf(inputVO.getCreatorId() != null, marketTemplateTable.creatorId().eq(inputVO.getCreatorId()))
                .where(Predicate.or(marketTemplateTable.type().eq(MarketTemplateTypeEnum.PUBLIC.getValue()),
                        Predicate.and(
                                marketTemplateTable.type().eq(MarketTemplateTypeEnum.PERSON.getValue()),
                                marketTemplateTable.creatorId().in(ownerIds)
                        )
                ))
                .orderBy(marketTemplateTable.id().desc())
                .select(marketTemplateTable).fetchPage(inputVO.getPageNo() - 1, inputVO.getPageSize());
    }

    default List<MarketTemplate> simpleList(List<Long> ownerIds){
        return sql().createQuery(marketTemplateTable)
                .where(Predicate.or(marketTemplateTable.type().eq(MarketTemplateTypeEnum.PUBLIC.getValue()),
                        Predicate.and(
                                marketTemplateTable.type().eq(MarketTemplateTypeEnum.PERSON.getValue()),
                                marketTemplateTable.creatorId().in(ownerIds)
                        )
                ))
                .orderBy(marketTemplateTable.id().desc())
                .select(marketTemplateTable)
                .execute();
    }

    default Optional<MarketTemplate> findDetailById(Long id){
        return sql().createQuery(marketTemplateTable)
                .where(marketTemplateTable.id().eq(id))
                .select(marketTemplateTable.fetch(MarketTemplateFetcher.$.allTableFields()
                        .resources(MarketTemplateResourceFetcher.$.allTableFields())
                        .content(MarketTemplateContentFetcher.$.allTableFields())))
                .fetchOptional();
    }

    Optional<MarketTemplate> findByNameAndCreatorId(String name, Long creatorId);


}